/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package azure;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;

/**
 * A custom JFrame extension that supports full-screen image views AND keyboard navigation
 * @author Gargravarr
 * @date Aug 23, 2011
 */
public class FullScreenView extends JFrame
{

    private Image mangaPage;
    private final MainView mv;
    /*Key Codes*/
    private final int RETURN_KEY = 10, ESCAPE_KEY = 27, SPACE_KEY = 32, LEFT_KEY = 37, UP_KEY = 38, RIGHT_KEY = 39, DOWN_KEY = 40, BACKSPACE_KEY = 8;
    private KeyListener fullScreenListener = new KeyListener() //Keyboard navigation
    {

	@Override
	public void keyTyped(KeyEvent e)
	{
	    //Ignore this, it's useless - can't get the damned keycodes out of it!
	}

	@Override
	/**
	 * Keyboard navigation acts on keyPress.
	 * Using the logic within MainView in the background, the key actions call the respective manga
	 * navigation method, extract the activeImage and draw it
	**/
	public void keyPressed(KeyEvent e)
	{
	    System.out.println("Key Pressed: " + e.getKeyCode());
	    switch (e.getKeyCode())
	    {
		case ESCAPE_KEY:
		{
		    dispose();
		    break;
		}
		case RETURN_KEY:
		{
		    clear();
		    mv.navigateToNextPage();
		    mangaPage = mv.getActiveImage();
		    paint(getGraphics());
		    break;
		}
		case BACKSPACE_KEY:
		{
		    clear();
		    mv.navigateToPreviousPage();
		    mangaPage = mv.getActiveImage();
		    paint(getGraphics());
		    break;
		}
		case SPACE_KEY:
		{
		    clear();
		    mv.navigateToNextPage();
		    mangaPage = mv.getActiveImage();
		    paint(getGraphics());
		    break;
		}
		case LEFT_KEY:
		{
		    clear();
		    mv.navigateToPreviousPage();
		    mangaPage = mv.getActiveImage();
		    paint(getGraphics());
		    break;
		}
		case RIGHT_KEY:
		{
		    clear();
		    mv.navigateToNextPage();
		    mangaPage = mv.getActiveImage();
		    paint(getGraphics());
		    break;
		}
		case UP_KEY:
		{
		    clear();
		    mv.navigateToFirstPage();
		    mangaPage = mv.getActiveImage();
		    paint(getGraphics());
		    break;
		}
		case DOWN_KEY:
		{
		    clear();
		    mv.navigateToLastPage();
		    mangaPage = mv.getActiveImage();
		    paint(getGraphics());
		    break;
		}
	    }
	}

	@Override
	public void keyReleased(KeyEvent e)
	{
	}
    };

    public FullScreenView(Image page, MainView mv)
    {
	this.mangaPage = page;
	this.mv = mv;
	// remove window frame  
	this.setUndecorated(true);

	// window should be visible 
	this.setVisible(true);

	// switching to fullscreen mode 
	GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);

	this.addMouseListener(new MouseListener()
	{

	    @Override
	    public void mouseClicked(MouseEvent e)
	    {
		dispose();
	    }

	    @Override
	    public void mousePressed(MouseEvent e)
	    {
	    }

	    @Override
	    public void mouseReleased(MouseEvent e)
	    {
	    }

	    @Override
	    public void mouseEntered(MouseEvent e)
	    {
	    }

	    @Override
	    public void mouseExited(MouseEvent e)
	    {
	    }
	});
	this.addKeyListener(fullScreenListener);
    }
    
    private void load(String fileName)
    {
	mangaPage = Toolkit.getDefaultToolkit().getImage(fileName);
    }

    public void paint(Graphics g)
    {
	if (mangaPage != null) // if image loaded and ready
	{
	    Dimension imageSize = getImageSize();
	    g.drawImage(mangaPage, // draw it  
		    (this.getWidth() / 2) - (imageSize.width / 2), // at the center  
		    0, // of screen
		    imageSize.width,
		    imageSize.height,
		    this);
	}
    }

    @Override
    public void dispose()
    {
	super.dispose();
    }

    public Dimension getImageSize()
    {
	Dimension newImageSize = new Dimension(mangaPage.getWidth(null), mangaPage.getHeight(null));
	Dimension displayDimensions = this.getSize();

//	double pageRatio = getImageRatio();
//	System.out.println("Page ratio: " + pageRatio);
//	if (newImageSize.height >= newImageSize.width) //Portrait ratio page
//	{
//	    newImageSize.width = displayDimensions.width;
//	    newImageSize.height = (int)(displayDimensions.height / pageRatio);
//	}
//	else //Landscape ratio page
//	{
//	    newImageSize.height = displayDimensions.height;
//	    newImageSize.width = (int)(displayDimensions.width / pageRatio);
//	}
	
	if (this.getHeight() >= this.getWidth()) //Screen is displaying portrait or square
	    {
		if (mangaPage.getHeight(null) >= mangaPage.getWidth(null)) //Page is also portrait or square
		{
		    Image temp = mangaPage.getScaledInstance(-1, this.getHeight(), java.awt.Image.SCALE_SMOOTH);
		    newImageSize.setSize(temp.getWidth(null), temp.getHeight(null));
		}
		else //Page is landscape
		{
		    Image temp = mangaPage.getScaledInstance(this.getWidth(), -1, java.awt.Image.SCALE_SMOOTH);
		    newImageSize.setSize(temp.getWidth(null), temp.getHeight(null));
		}
	    }
	    else //Screen is landscape
	    {
		Image temp = mangaPage.getScaledInstance(-1, this.getHeight(), java.awt.Image.SCALE_SMOOTH);
		newImageSize.setSize(temp.getWidth(null), temp.getHeight(null));
	    }
	System.out.println("Image Size: " + newImageSize.width + "x" + newImageSize.height);
	return newImageSize;
    }

    public double getImageRatio()
    {
	if (mangaPage.getHeight(null) >= mangaPage.getWidth(null))
	{
	    return (((double)mangaPage.getWidth(null)) / ((double)mangaPage.getHeight(null)));
	}
	else
	{
	    return (((double)mangaPage.getHeight(null)) / ((double)mangaPage.getWidth(null)));
	}
    }
    
    /**
     * Clear the drawing surface
     * TODO: This is annoyingly slow; need to find another way
    **/
    public void clear()
    {
	this.getGraphics().clearRect(0, 0, this.getWidth(), this.getHeight());
    }
}
